// Copyright (C) 2024 Shpz<shaaapz@gmail.com>. All Rights Reserved.

import { Component, EventKeyboard, Input, KeyCode, Prefab, _decorator, find, input, instantiate } from 'cc';
import { Pawn } from './Pawn';
import { Spectator } from './Spectator';

const { ccclass, property } = _decorator;

export enum EControlMode {
    Player,
    Spectator
}

@ccclass('Level')
export class Level extends Component {

    private controlMode = EControlMode.Player;

    start() {
        input.on(
            Input.EventType.KEY_DOWN,
            this.onPressed,
            this
        );

        let defaultPawn = find("DefaultPawn").getComponent(Pawn);
        defaultPawn.possess();
    }

    protected onPressed(event: EventKeyboard) {
        const keyCode = event.keyCode;
        switch (keyCode) {
            case KeyCode.KEY_Q:
                if (this.controlMode === EControlMode.Player) {
                    this.controlMode = EControlMode.Spectator;

                    let defaultPawn = find("DefaultPawn").getComponent(Pawn);
                    defaultPawn.unpossess();

                    let sepctator = find("Spectator").getComponent(Spectator);
                    sepctator.possess();
                }
                else {
                    this.controlMode = EControlMode.Player;
                    let sepctator = find("Spectator").getComponent(Spectator);
                    sepctator.unpossess();

                    let defaultPawn = find("DefaultPawn").getComponent(Pawn);
                    defaultPawn.possess();
                }

            case KeyCode.KEY_P:

                break;
        }
    }
}